﻿using Lhz.Data;
using Lhz.Data.Linq;
using Lhz.Data.Linq.Nh;
using System.Collections.Generic;
using System.Linq;

namespace Lhz.Common.Business
{
    public class EntityProxy : IEntityProxy
    {
        protected NhProxy nhProxy;

        public EntityProxy(string pathConfiguration = null)
        {
            nhProxy = new NhProxy(pathConfiguration);
        }

        public IQueryable<TEntity> CreateQueryable<TEntity>() where TEntity : class
        {
            return nhProxy.Query<TEntity>();
        }

        public virtual void ClearCache()
        {
            nhProxy.ClearCache();
        }

        public virtual void Dispose()
        {
            nhProxy.Dispose();
        }
    }

    public class EntityProxy<TPk, TEntity> : EntityProxy, IEntityProxy<TPk, TEntity> where TEntity : class
    {
        public EntityProxy(string pathConfiguration = null) : base(pathConfiguration) { }

        public IQueryable<TEntity> CreateQueryable()
        {
            return CreateQueryable<TEntity>();
        }

        public virtual List<TEntity> Query()
        {
            return CreateQueryable().ToList();
        }

        public virtual List<TEntity> Query(Pagination pagination)
        {
            return CreateQueryable().Execute(pagination);
        }

        public virtual List<TEntity> Query(List<OrderBy> orderBies)
        {
            return CreateQueryable().OrderBy(orderBies).ToList();
        }

        public virtual List<TEntity> Query(Pagination pagination, List<OrderBy> orderBies)
        {
            return CreateQueryable().OrderBy(orderBies).Execute(pagination);
        }

        public virtual TEntity QueryByPk(TPk pk)
        {
            return nhProxy.QueryByPk<TPk, TEntity>(pk);
        }

        public virtual void Add(TEntity item)
        {
            nhProxy.Add(item);
            nhProxy.SaveChanges();
        }

        public virtual void Add(IEnumerable<TEntity> items)
        {
            nhProxy.Add(items);
            nhProxy.SaveChanges();
        }

        public virtual void Update(TEntity item)
        {
            nhProxy.Update(item);
            nhProxy.SaveChanges();
        }

        public virtual void Update(IEnumerable<TEntity> items)
        {
            nhProxy.Update(items);
            nhProxy.SaveChanges();
        }

        public virtual void Delete(TEntity item)
        {
            nhProxy.Delete(item);
            nhProxy.SaveChanges();
        }

        public virtual void Delete(IEnumerable<TEntity> items)
        {
            nhProxy.Delete(items);
            nhProxy.SaveChanges();
        }

        public virtual void DeleteByPk(TPk pk)
        {
            nhProxy.DeleteByPk<TPk, TEntity>(pk);
            nhProxy.SaveChanges();
        }

        public virtual void DeleteByPk(IEnumerable<TPk> pks)
        {
            foreach (var pk in pks)
            {
                nhProxy.DeleteByPk<TPk, TEntity>(pk);
            }
            nhProxy.SaveChanges();
        }
    }
}
